﻿SSVC.Wrap.Dojo.Declare("ActiveTextElement", SSVC.SVG.ActiveElement,
{
	TextArea: null,
	TextElementHelper: null,
	TextAreaHeight: null,

	_textRows: 1,

	OnMouseUp: function(callElement)
	{
		if (callElement.ActionMode == SSVC.SVG.ActiveElement.Enums.ActionMode.Drawing)
		{
			callElement.TextArea.textContent = "";
			callElement.TextArea.style.width = +callElement.OX + "px";

			callElement.TextArea.focus();
		}
		callElement.inherited(arguments);
		callElement.Node.zIndex = "5000000";
	},

	OnMouseDblClick: function(callElement)
	{
		callElement.TextArea.focus();
	},

	OnTextAreaKeyDown: function(callElement)
	{
		callElement._resizeTextHeight();
		callElement.Refresh();

		if (arguments[1].keyCode == 13 && !arguments[1].ctrlKey && !arguments[1].shiftKey)
		{
			callElement.TextArea.blur();
			callElement.SelectMode = SSVC.SVG.ActiveElement.Enums.SelectMode.None;
			callElement.Refresh();
		}
	},

	OnTextAreaKeyUp: function(callElement)
	{
		callElement._resizeTextHeight();
		callElement.Refresh();
	},

	OnTextAreaFocus: function(callElement)
	{
		callElement._setTextAreaTop();
		callElement.Refresh();
	},

	OnTextAreaBlur: function(callElement)
	{
		callElement._setTextAreaBack();
	},

	_resizeTextHeight: function()
	{
		if (this.TextArea.scrollHeight + this.offset === 0) return;
		this.TextArea.style.height = "auto";
		this.TextArea.style.height = (this.TextArea.scrollHeight + this.offset) + "px";
		this.OY = (this.TextArea.scrollHeight + this.offset);
	},

	_addInnerElement: function()
	{
		this.TextArea = document.createElement("textArea");
		document.body.appendChild(this.TextArea);
		this.TextArea.className = "EditableTextArea";

		this.TextArea.id = "EditableTextArea_" + SSVC.SVG.Shape.MakeName(6);
		this.TextArea.setAttribute("rows", 1);
		this.TextArea.setAttribute("wrap", "soft");
		this.TextArea.style.display = "none";
		this.TextArea.textContent = SSVC.Utils.GetResource("feed_tb_def_comment");

		this.offset = !window.opera ? (this.TextArea.offsetHeight - this.TextArea.clientHeight) : (this.TextArea.offsetHeight + parseInt(window.getComputedStyle(this.TextArea, null).getPropertyValue("border-top-width")));

		this.TextArea.addEventListener("keydown", SSVC.Wrap.Dojo.Partial(this.OnTextAreaKeyDown, this));
		this.TextArea.addEventListener("keyup", SSVC.Wrap.Dojo.Partial(this.OnTextAreaKeyUp, this));
		SSVC.Wrap.Dojo.On(this.TextArea, "focus", SSVC.Wrap.Dojo.Partial(this.OnTextAreaFocus, this));
		SSVC.Wrap.Dojo.On(this.TextArea, "blur", SSVC.Wrap.Dojo.Partial(this.OnTextAreaBlur, this));

	},

	_addSelection: function()
	{
		this.Contour = new SSVC.SVG.Rectangle();
		this.Contour.Class = "Contour";

		this.inherited(arguments);
		this.Frame.Class = "Frame";
	},

	_arrangeGroupNode: function()
	{
		this.Node.appendChild(this.Contour.Node);
		this.Node.appendChild(this.Frame.Node);

		for (var i in this.Tops)
			this.Node.appendChild(this.Tops[i].Node);
	},

	_arrangeInnerElements: function()
	{
		this.Contour.update(this.X, this.Y, this.OX, this.OY);
		this.Frame.update(this.X, this.Y, this.OX, this.OY);
		this._updateTextControl();
	},

	_updateTextControl: function()
	{
		var rect = this.Frame.Node.getBoundingClientRect();
		this.TextArea.style.top = +rect.top + "px";
		this.TextArea.style.left = +rect.left + "px";
		this.TextArea.style.color = this.LineColor;

		if (this.SelectMode == SSVC.SVG.ActiveElement.Enums.SelectMode.Contoured && this.ActionMode == SSVC.SVG.ActiveElement.Enums.ActionMode.Drawing)
		{
			this.TextArea.style.fontSize = this.OY * 0.85 + "px";
			this.TextArea.style.lineHeight = this.Frame.OY + "px";
			this.TextArea.style.fontFamily = this.FontFamily;
			this.FontSize = this.TextArea.style.fontSize;
		}
		else
		{
			this.TextArea.style.width = this.OX + "px";
			this._resizeTextHeight();
		}
	},

	_refreshSelection: function()
	{
		switch (this.SelectMode)
		{
			case SSVC.SVG.ActiveElement.Enums.SelectMode.Contoured:
				this._showContour();
				this._showInnerElement();
				this._hideSelection();
				break;
			case SSVC.SVG.ActiveElement.Enums.SelectMode.Selected:
				this._showSelection();
				this._showInnerElement();
				this._hideContour();
				break;
			case SSVC.SVG.ActiveElement.Enums.SelectMode.None:
				this._hideContour();
				this._hideSelection();
				this._showInnerElement();
				break;
		}

	},

	_setTextAreaTop: function()
	{
		this.TextArea.style.zIndex = "100500";
	},
	_setTextAreaBack: function()
	{
		this.TextArea.style.zIndex = "100";
	},

	_showInnerElement: function()
	{
		this.TextArea.style.display = "block";
	},

	_hideInnerElement: function()
	{
		this.TextArea.style.display = "none";
	},

	_disableSelection: function(el)
	{
		el.addEventListener("selectstart", this._disabler);
	},

	_disabler: function(e)
	{
		if (e.preventDefault)
		{
			e.preventDefault();
		}
		return false;
	},

	_enableSelection: function(el)
	{
		el.removeEventListener("selectstart", this._disabler);
	},

	GetOuterXml: function()
	{
		/* We changed body of this function by removing <textarea> tag since it caused error
		"Object doesn't support property or method "measureText" " */
		var properties = [];

		var attrXml = "";
		for (var i = 0; i < this.Node.attributes.length; i++)
		{
			attrXml += "{0}='{1}' ".Format(this.Node.attributes[i].name, this.Node.attributes[i].value);
		}

		for (i = 0; i < this.TextArea.attributes.length; i++)
		{
			if (this.TextArea.attributes[i].name != "id")
				attrXml += "{0}='{1}' ".Format(this.TextArea.attributes[i].name, this.TextArea.attributes[i].value);
		}

		this.innerXml = "<{0} {1} text-height='{3}'>{2}</{0}>".Format("text", attrXml.trim(), this.TextArea.textContent, this.TextArea.style.height);

		var resultxml = SSVC.SVG.ShapePrototype.ParseXml(this.innerXml);

		return resultxml;

		//var properties = [];

		//var attrXml = "";
		//for (var i = 0; i < this.Node.attributes.length; i++)
		//{
		//	attrXml += "{0}='{1}' ".Format(this.Node.attributes[i].name, this.Node.attributes[i].value);
		//}
		//this.innerXml = "<{0} {1} text-height='{3}'>{2}</{0}>".Format("text", attrXml.trim(), this.TextArea.textContent, this.TextArea.style.height);

		//var textareaattrs = "";
		//for (var i = 0; i < this.TextArea.attributes.length; i++)
		//{
		//	textareaattrs += "{0}='{1}' ".Format(this.TextArea.attributes[i].name, this.TextArea.attributes[i].value);
		//}
		//var textareaxml = "<{0} {1}>{2}</{0}>".Format("textarea", textareaattrs.trim(), this.TextArea.textContent);

		//var resultxml = SSVC.SVG.ShapePrototype.ParseXml(this.innerXml);
		//var textarea = SSVC.SVG.ShapePrototype.ParseXml(textareaxml);
		//resultxml.appendChild(textarea);

		//return resultxml;
	},

	SetXml: function(xmldoc)
	{
		this.applyXml(xmldoc);

		this.TextArea.style.fontSize = this.OY * 0.85 + "px";
		this.TextArea.style.lineHeight = this.OY + "px";
		this.TextArea.style.color = this.LineColor;
		this.TextArea.style.height = xmldoc.getAttribute("text-height");

		this.TextArea.textContent = xmldoc.text;
		this.Refresh();
	},

	SetLineProperties: function(width, color, dash)
	{
		this.LineColor = color;
		this.LineDash = dash;
		this.LineWidth = width;
	},

	Fill: function(rgb)
	{
		this.TextArea.style.backgraund = rgb;
	}
});
SSVC.Wrap.Dojo.SetObject("SSVC.SVG.ActiveTextElement", ActiveTextElement);
